#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author : hai.long
import pyautogui
from time import sleep
import pygetwindow as gw
from utils import log_tool
from pywinauto import Application


def get_all_window():
    windows = gw.getAllWindows()
    return [window.title for window in windows]


def get_window(win_title):
    # 获取当前所有窗口
    windows = gw.getAllWindows()
    windows_title = []
    for window in windows:
        if win_title in window.title:
            windows_title.append(window.title)
    if len(windows_title) > 0:
        window1 = gw.getWindowsWithTitle(windows_title[0])[0]
        return window1
    else:
        return None


def get_many_window(win_title):
    # 获取当前所有窗口
    windows = gw.getAllWindows()
    windows_list = []
    for window in windows:
        if win_title in window.title:
            window1 = gw.getWindowsWithTitle(window.title)[0]
            windows_list.append(window1)
    return windows_list


def get_window_title(win_title):
    # 获取当前所有窗口
    windows = gw.getAllWindows()
    windows_title = []
    for window in windows:
        if win_title in window.title:
            windows_title.append(window.title)
    if len(windows_title) > 0:
        window1 = gw.getWindowsWithTitle(windows_title[0])[0].title
        return window1
    else:
        return None


def activate_window(title):
    if title:
        # 获取窗口句柄
        app = Application().connect(title=title)
        window = app.top_window()
        # 切换窗口
        window.set_focus()


def max_window():
    # 最大化录制浏览器Chromium
    for i in range(2):
        window = get_window("Chromium")
        if window:
            window.maximize()
            break
        sleep(1)
    # 关闭Playwright Inspector窗口
    for i in range(2):
        inspector = get_window("Playwright Inspector")
        if inspector:
            inspector.close()
            log_tool.info("关闭Playwright Inspector窗口")
            break
        sleep(1)


# 关闭Chromium浏览器
def close_window():
    sleep(1)
    window = get_window("Chromium")
    if window:
        window.close()
